/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lanzamientoparaboico;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Usuario
 */
public class miPanel extends javax.swing.JPanel {

    Movimientos m;
    NumberFormat formatter = new DecimalFormat("#0.00");
    int xSubida = 0;
    int ySubida = 500;
    int distanciaX;
    int distanciaY;
    String url = "src\\images\\1.png";
    Timer jugar;
    DefaultTableModel modelo;

    public miPanel() {
        initComponents();
        this.setSize(1400, 650);
        this.setBackground(Color.white);
        modelo = new DefaultTableModel();
        modelo = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int fila, int columna) {
                return false;
            }
        };
        modelo.addColumn("Angulo");
        modelo.addColumn("Velocidad");
        modelo.addColumn("Altura Tope");
        modelo.addColumn("Tiempo en el aire");
        modelo.addColumn("Distancia recorrida X");
        modelo.addColumn("Velocidad X");
        modelo.addColumn("Velocidad Y");
        jTable1.setModel(modelo);
        jugar = new Timer(50, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                moverBala();
                if ((500 - m.getAlturaMax()) > ySubida) {
                    url = "src\\images\\3.png";
                }
                if(ySubida>500){
                    url="src\\images\\fin.gif";
                    jButton1.setText("Play");
                    actualizarTabla();
                    jugar.stop();
                }
                repaint();
            }
        });
    }
    
    public void actualizarTabla(){
        Object[] o = new Object[modelo.getColumnCount()];
            o[0] = jTextField1.getText();
            o[1] = jTextField2.getText();
            o[2] = formatter.format(m.getAlturaMax());
            o[3] = formatter.format(m.getTiempoAire());
            o[4] = formatter.format(m.getDistanciaLlegada());
            o[5] = formatter.format(m.getVelocidadX());
            o[6] = formatter.format(m.getVelocidadY());
       
            modelo.addRow(o);
        
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(Toolkit.getDefaultToolkit().getImage(url), xSubida, ySubida, this);
    }

    public void moverBala() {
        if (url.equals("src\\images\\1.png") || url.equals("src\\images\\2.png")) {
            xSubida += distanciaX;
            ySubida -= distanciaY;
            if (url.equals("src\\images\\1.png")) {
                url = "src\\images\\2.png";
            } else {
                url = "src\\images\\1.png";
            }
        } else {
            xSubida += distanciaX;
            ySubida += distanciaY;
            if (url.equals("src\\images\\3.png")) {
                url = "src\\images\\4.png";
            } else {
                url = "src\\images\\3.png";
            }
        }

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jButton1.setText("Play");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Angulo: ");

        jLabel2.setText("Velocidad: ");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(41, 41, 41)))
                .addGap(21, 21, 21))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 41, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 164, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addComponent(jButton1)
                .addGap(45, 45, 45))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(jTextField1.getText().equals("")||jTextField2.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Llene ambos campos");
            return;
        }
        if (jButton1.getText().equals("Play")) {
            m = new Movimientos(Integer.parseInt(jTextField1.getText()), Double.parseDouble(jTextField2.getText()));
            distanciaX = (int) m.getVelocidadX();
            distanciaY = (int) m.getVelocidadY();
            xSubida=0;
            ySubida=500;
            url = "src\\images\\1.png";
            jugar.start();
            jButton1.setText("Stop");
        } else {
            jugar.stop();
            jButton1.setText("Play");
        }
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
